//
//  SPDeck.m
//  ShandongPoker
//
//  Created by Developer on 2025-01-08.
//  Copyright © 2025 ShandongPoker. All rights reserved.
//

#import "SPDeck.h"

@interface SPDeck ()

@property (nonatomic, strong, readwrite) NSArray<SPCard *> *allCards;
@property (nonatomic, strong, readwrite) NSMutableArray<SPCard *> *availableCards;
@property (nonatomic, strong, readwrite) NSMutableArray<SPCard *> *dealtCards;
@property (nonatomic, assign, readwrite) BOOL isShuffled;

@end

@implementation SPDeck

#pragma mark - Initialization

- (instancetype)init {
    return [self initWithDeckCount:1];
}

- (instancetype)initWithDeckCount:(NSInteger)deckCount {
    if (self = [super init]) {
        _allCards = [self createCardsWithDeckCount:deckCount];
        _availableCards = [NSMutableArray arrayWithArray:_allCards];
        _dealtCards = [[NSMutableArray alloc] init];
        _isShuffled = NO;
    }
    return self;
}

#pragma mark - Private Methods

- (NSArray<SPCard *> *)createCardsWithDeckCount:(NSInteger)deckCount {
    NSMutableArray<SPCard *> *cards = [[NSMutableArray alloc] init];
    
    for (NSInteger deck = 0; deck < deckCount; deck++) {
        // 创建所有花色和点数的牌
        for (SPCardSuit suit = SPCardSuitSpades; suit <= SPCardSuitClubs; suit++) {
            for (SPCardRank rank = SPCardRankTwo; rank <= SPCardRankAce; rank++) {
                SPCard *card = [[SPCard alloc] initWithSuit:suit rank:rank];
                [cards addObject:card];
            }
        }
    }
    
    return [cards copy];
}

#pragma mark - Public Methods

- (void)shuffle {
    // 使用Fisher-Yates洗牌算法
    NSInteger count = self.availableCards.count;
    
    for (NSInteger i = count - 1; i > 0; i--) {
        NSInteger j = arc4random_uniform((uint32_t)(i + 1));
        [self.availableCards exchangeObjectAtIndex:i withObjectAtIndex:j];
    }
    
    self.isShuffled = YES;
}

- (SPCard *)dealCard {
    if (self.availableCards.count == 0) {
        return nil;
    }
    
    SPCard *card = [self.availableCards firstObject];
    [self.availableCards removeObjectAtIndex:0];
    [self.dealtCards addObject:card];
    
    return card;
}

- (NSArray<SPCard *> *)dealCards:(NSInteger)count {
    NSMutableArray<SPCard *> *dealtCards = [[NSMutableArray alloc] init];
    
    for (NSInteger i = 0; i < count; i++) {
        SPCard *card = [self dealCard];
        if (card) {
            [dealtCards addObject:card];
        } else {
            break; // 没有更多牌了
        }
    }
    
    return [dealtCards copy];
}

- (void)reset {
    [self.availableCards removeAllObjects];
    [self.availableCards addObjectsFromArray:self.allCards];
    [self.dealtCards removeAllObjects];
    self.isShuffled = NO;
}

- (BOOL)isEmpty {
    return self.availableCards.count == 0;
}

- (NSInteger)remainingCards {
    return self.availableCards.count;
}

- (SPCard *)cardAtIndex:(NSInteger)index {
    if (index >= 0 && index < self.availableCards.count) {
        return self.availableCards[index];
    }
    return nil;
}

- (void)returnCardToTop:(SPCard *)card {
    if (card && [self.dealtCards containsObject:card]) {
        [self.dealtCards removeObject:card];
        [self.availableCards insertObject:card atIndex:0];
    }
}

- (void)returnCardToBottom:(SPCard *)card {
    if (card && [self.dealtCards containsObject:card]) {
        [self.dealtCards removeObject:card];
        [self.availableCards addObject:card];
    }
}

#pragma mark - NSObject

- (NSString *)description {
    return [NSString stringWithFormat:@"SPDeck: %ld cards remaining, shuffled: %@", 
            (long)self.remainingCards, self.isShuffled ? @"YES" : @"NO"];
}

@end 